package com.jai.pms.service.impl;

import java.util.List;
import java.util.Objects;

import com.jai.pms.domain.Project;
import com.jai.pms.domain.Tool;
import com.jai.pms.mapper.ProjectMapper;
import com.jai.pms.mapper.ToolMapper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jai.pms.mapper.ProjectToolMapper;
import com.jai.pms.domain.ProjectTool;
import com.jai.pms.service.IProjectToolService;

/**
 * 项目工具管理Service业务层处理
 * 
 * @author v
 * @date 2023-08-23
 */
@Service
public class ProjectToolServiceImpl implements IProjectToolService 
{
    @Autowired
    private ProjectToolMapper projectToolMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ToolMapper toolMapper;

    /**
     * 查询项目工具管理
     * 
     * @param id 项目工具管理主键
     * @return 项目工具管理
     */
    @Override
    public ProjectTool selectProjectToolById(Long id)
    {
        return projectToolMapper.selectProjectToolById(id);
    }

    /**
     * 查询项目工具管理列表
     * 
     * @param projectTool 项目工具管理
     * @return 项目工具管理
     */
    @Override
    public List<ProjectTool> selectProjectToolList(ProjectTool projectTool)
    {
        return projectToolMapper.selectProjectToolList(projectTool);
    }

    /**
     * 新增项目工具管理
     * 
     * @param projectTool 项目工具管理
     * @return 结果
     */
    @Override
    public int insertProjectTool(ProjectTool projectTool)
    {
        preCheck(projectTool, true);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectTool.setCreateBy(loginUser.getUsername());
        projectTool.setCreateTime(DateUtils.getNowDate());

        return projectToolMapper.insertProjectTool(projectTool);
    }

    /**
     * 修改项目工具管理
     * 
     * @param projectTool 项目工具管理
     * @return 结果
     */
    @Override
    public int updateProjectTool(ProjectTool projectTool)
    {
        preCheck(projectTool, false);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectTool.setUpdateBy(loginUser.getUsername());
        projectTool.setUpdateTime(DateUtils.getNowDate());

        return projectToolMapper.updateProjectTool(projectTool);
    }

    /**
     * 批量删除项目工具管理
     * 
     * @param ids 需要删除的项目工具管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectToolByIds(Long[] ids)
    {
        return projectToolMapper.deleteProjectToolByIds(ids);
    }

    /**
     * 删除项目工具管理信息
     * 
     * @param id 项目工具管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectToolById(Long id)
    {
        return projectToolMapper.deleteProjectToolById(id);
    }


    public ProjectTool preCheck(ProjectTool projectTool, boolean isAdd) {
        // 检查是否有重复信息，联合主键projectId和dataId
        ProjectTool query = new ProjectTool();
        query.setProjectId(projectTool.getProjectId());
        query.setToolId(projectTool.getToolId());
        List<ProjectTool> queryList = projectToolMapper.selectProjectToolList(query);
        if (isAdd) {
            if (queryList.size() > 0) {
                throw new ServiceException("该工具和版本在项目中已经存在");
            }
        } else {
            if (queryList.size() > 0 && !Objects.equals(queryList.get(0).getId(), projectTool.getId())) {
                throw new ServiceException("该工具和版本在项目中已经存在");
            }
        }

        // 填充project的code、name信息
        Project project = projectMapper.selectProjectById(projectTool.getProjectId());
        projectTool.setProjectCode(project.getCode());
        projectTool.setProjectName(project.getName());

        // 填充tool的code、version 、name信息
        Tool tool = toolMapper.selectToolById(projectTool.getToolId());
        projectTool.setToolCode(tool.getCode());
        projectTool.setToolVersion(tool.getVersion());
        projectTool.setToolName(tool.getName());

        return projectTool;
    }
}
